package com.leetcode.concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntConsumer;

public class FizzBuzzReentrantConds {

    private final int n;

    private static final int NUMBER = 0;
    private static final int FIZZ = 3;
    private static final int BUZZ = 5;
    private static final int FIZZBUZZ = 15;

    int state = NUMBER;

    private final ReentrantLock lock = new ReentrantLock();
    private final Condition numCondition = lock.newCondition();
    private final Condition fizzCondition = lock.newCondition();
    private final Condition buzzCondition = lock.newCondition();
    private final Condition fbCondition = lock.newCondition();

    public FizzBuzzReentrantConds(int n) {
        this.n = n;
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 3; i <= n; i += 3) {
            lock.lock();
            try {
                if (i % 15 == 0) {
                    continue;
                }
                while (state != FIZZ) {
                    fizzCondition.await();
                }
                printFizz.run();
                state = NUMBER;
                numCondition.signal();
            } finally {
                lock.unlock();
            }
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 5; i <= n; i += 5) {
            lock.lock();
            try {
                if (i % 15 == 0) {
                    continue;
                }
                while (state != BUZZ) {
                    buzzCondition.await();
                }
                printBuzz.run();
                state = NUMBER;
                numCondition.signal();
            } finally {
                lock.unlock();
            }
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 15; i <= n; i += 15) {
            lock.lock();
            try {
                while (state != FIZZBUZZ) {
                    fbCondition.await();
                }
                printFizzBuzz.run();
                state = NUMBER;
                numCondition.signal();
            } finally {
                lock.unlock();
            }
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            lock.lock();
            try {
                while (state != NUMBER) {
                    numCondition.await();
                }
                if (i % 3 != 0 && i % 5 != 0) {
                    printNumber.accept(i);
                } else {
                    if (i % 15 == 0) {
                        state = FIZZBUZZ;
                        fbCondition.signal();
                    }else if (i % 3 == 0) {
                        state = FIZZ;
                        fizzCondition.signal();
                    }else if (i % 5 == 0) {
                        state = BUZZ;
                        buzzCondition.signal();
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {

        FizzBuzz fizzBuzz = new FizzBuzz(15);

        Thread fizz = new Thread(() -> {
            try {
                fizzBuzz.fizz(() -> System.out.println("fizz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread buzz = new Thread(() -> {
            try {
                fizzBuzz.buzz(() -> System.out.println("buzz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread fizzbuzz = new Thread(() -> {
            try {
                fizzBuzz.fizzbuzz(() -> System.out.println("fizzbuzz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread number = new Thread(() -> {
            try {
                fizzBuzz.number(System.out::println);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        fizz.start();
        buzz.start();
        fizzbuzz.start();
        number.start();

    }
}
